package com.js.auth.server.jwt.user;

import com.js.auth.server.config.KeyConfig;
import com.js.core.util.jwt.IJwtInfo;
import com.js.core.util.jwt.JwtHelper;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * @author ygl
 * @create 2018-10-29
 * @DESCRIPTION
 **/
@Component
public class JwtTokenUtil {

    @Getter
    @Value("${jwt.expire}")
    private int expire;

    @Autowired
    private KeyConfig keyConfig;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 通过私钥加密 生成token
     * @param jwtInfo
     * @param otherInfo
     * @param expireTime
     * @return
     * @throws Exception
     */
    public String generateToken(IJwtInfo jwtInfo, Map<String, String> otherInfo, Date expireTime) throws Exception {
        return jwtHelper.generateToken(jwtInfo, keyConfig.getUserPriKey(), expireTime, otherInfo);
    }

    /**
     * 通过公钥解密 token获取信息
     * @param token
     * @return
     * @throws Exception
     */
    public IJwtInfo getInfoFromToken(String token) throws Exception {
        IJwtInfo infoFromToken = jwtHelper.getInfoFromToken(token, keyConfig.getUserPubKey());
        return infoFromToken;
    }
}
